//
//  GridTabBarController.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/3/17.
//

import UIKit
import ESTabBarController_swift

class GridTabBarController: ESTabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .white

        tabBar.isTranslucent = false
        tabBar.backgroundImage = UIImage(color: Theme.Color.TabBar.background, size: CGSize(width: 1, height: 1))
        tabBar.shadowImage = UIImage(color: Theme.Color.TabBar.shadow, size: CGSize(width: 1, height: 1))
        tabBar.layer.shadowOffset = CGSize(width: 0, height: 1)
        tabBar.layer.shadowRadius = 0
        tabBar.layer.shadowColor = Theme.Color.TabBar.shadow.cgColor
        tabBar.layer.shadowOpacity = 1.0

        let tabBar = (self.tabBar as! ESTabBar)
        tabBar.itemCustomPositioning = .fillExcludeSeparator
        tabBar.itemEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 6, right: 0)
    }
    
    @available(iOS 11.0, *)
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        if view.safeAreaInsets.bottom == 0 {
            (tabBar as! ESTabBar).itemEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 6, right: 0)
        } else {
            (tabBar as! ESTabBar).itemEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: view.safeAreaInsets.bottom, right: 0)
        }
        (tabBar as! ESTabBar).layoutSubviews()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

final class TabBarHighlightableContentView: ESTabBarItemContentView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        renderingMode = .alwaysOriginal
        imageView.contentMode = .center

        backgroundColor = .clear
        textColor = Theme.Color.TabBar.itemNormal
        highlightTextColor = Theme.Color.TabBar.itemSelected
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func updateLayout() {
        super.updateLayout()
        superview?.backgroundColor = .clear
    }
}

